import time
from gen347 import generate_test_case
from alg347 import topKFrequent

def is_valid_result(nums, k, result):
    from collections import Counter
    count = Counter(nums)
    freq_sorted = sorted(count.items(), key=lambda x: x[1], reverse=True)
    top_k = set([item[0] for item in freq_sorted[:k]])
    return set(result).issubset(top_k) and len(result) == k

def run_test():
    nums, k = generate_test_case(1000, (1, 50))
    print(f"测试数组长度: {len(nums)}, k: {k}")
    start_time = time.time()
    result = topKFrequent(nums, k)
    end_time = time.time()

    print("返回结果：", result)
    print("测试是否通过：", "✔️" if is_valid_result(nums, k, result) else "❌")
    print("运行时间：%.6f 秒" % (end_time - start_time))

if __name__ == "__main__":
    run_test()
